﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CED_3DPoints.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		Dialog for 3D points settings
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports System.Windows.Forms
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '




'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '

Public Class CED_3DPoints

    '// --------------------------------------------------------------------- '
    '// DATA

    '// ===================================================================== '




    ' /////////////////////////////////////////////////////////////////////////
#Region "Private functions"

#End Region
    ' /////////////////////////////////////////////////////////////////////////





    ' /////////////////////////////////////////////////////////////////////////
#Region "Public functions"

#End Region
    ' /////////////////////////////////////////////////////////////////////////





    ' /////////////////////////////////////////////////////////////////////////
#Region "Event handlers"


    '// --------------------------------------------------------------------- '
    Private Sub OK_Button_Click(ByVal sender As System.Object, _
                                ByVal e As System.EventArgs) _
                                Handles OK_Button.Click

        Try
            Dim lcSTG As New PT3DSTG

            ' create a local copy of global settings
            lcSTG = CM_3DPoints.Stgs

            '// Layers
            lcSTG.lyCode = cbLyCode.Text
            lcSTG.lyMark = cbLyMark.Text
            lcSTG.lyNr = cbLyNr.Text
            lcSTG.lyZ = cbLyZ.Text

            '// Blocks
            lcSTG.sBlockName = CB_BlkName.Text


            '// Text
            lcSTG.sTxSty = CB_TxSty.Text
            lcSTG.txH = CDbl(TX_TXH.Text)

            '// 0 - no reset
            '// 1 - reset
            '// 2 - only moved
            '// 3 - smart
            If CK_ResetPos.Checked = True Then
                lcSTG.iRstPos = 1
            ElseIf CK_OnlyMoved.Checked = True Then
                lcSTG.iRstPos = 2
            ElseIf CK_DontReset.Checked = True Then
                lcSTG.iRstPos = 0
            Else'If CK_SmartReset.Checked = True Then
                lcSTG.iRstPos = 3
            End If

            '// Check a bit what was entered
            If (lcSTG.lyMark = "") Then
                lcSTG.lyMark = "0"
            End If
            If (lcSTG.lyNr = "") Then
                lcSTG.lyNr = "0"
            End If
            If (lcSTG.lyZ = "") Then
                lcSTG.lyZ = "0"
            End If
            If (lcSTG.lyCode = "") Then
                lcSTG.lyCode = "0"
            End If
            If (lcSTG.sBlockName = "") Then
                lcSTG.sBlockName = "PCT3D"
            End If
            Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices. _
                Application.DocumentManager.MdiActiveDocument
            Using scDocL As DocumentLock = acDoc.LockDocument()
                CM_3DPoints.EditBlocks(acDoc,lcSTG)
            End Using
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
            
        Catch ex As Exception
		    Dim trace As System.Diagnostics.StackTrace = _
                New System.Diagnostics.StackTrace(ex, True)
		    Dim exceptionLineNumber As Integer =  _
                trace.GetFrame(0).GetFileLineNumber()
		    MsgBox(CMain._T(TX.EXC_IN) & trace.GetFrame(0).GetMethod.Name & vbCrLf & _
		     CStr(ex.Message) & "; " & vbCrLf & _
		     trace.GetFrame(0).ToString & vbCrLf)
        End Try

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub Cancel_Button_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles Cancel_Button.Click

        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CED_3DPoints_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load



        '----------- [ Static text ] --------------------
        Me.Text = CMain._T(TX.FSK_PT3D) & ". " & _
                CMain._T(TX.EDIT) & "."
        Me.OK_Button.Text = CMain._T(TX.OK)
        Me.Cancel_Button.Text = CMain._T(TX.CANCEL)

        '// Labels for tab
        Me.TabP_Block.Text = CMain._T(TX.BLOCK)
        Me.TabP_Lay.Text = CMain._T(TX.LAYS)
        Me.TabP_Text.Text = CMain._T(TX.TEXT)


        '// Layers
        Me.L_Mark.Text = CMain._T(TX.MARKER)
        Me.L_NrPt.Text = CMain._T(TX.PTNR)
        Me.L_Descr.Text = CMain._T(TX.DESCR)
        Me.L_Height.Text = CMain._T(TX.HEIGHT)

        Me.L_BlkName.Text = CMain._T(TX.BLKNAME)

        '// Text
        Me.L_Style.Text = CMain._T(TX.STYLE)
        Me.L_TxH.Text = CMain._T(TX.HTEXT)

        Me.CK_ResetPos.Text = CMain._T(TX.PTS_RSTPOS)
        Me.CK_OnlyMoved.Text = CMain._T(TX.PTS_RSTUNM)
        Me.CK_DontReset.Text = CMain._T(TX.PTS_NORST)
        Me.CK_SmartReset.Text = CMain._T(TX.PTS_RSTSMART)



        '----------- [ Actual settings ] --------------------
        Dim acDoc As Document = _
        Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        If (acDoc is Nothing) tHEN
            Me.Close()
            Return
        End If
        Dim acCurDb As Database = acDoc.Database
        Using acTrans As Transaction = _
            acCurDb.TransactionManager.StartTransaction()
        Try
            
            '// returns the layer table for the current database
            Dim acLyrTbl As LayerTable = _
            DirectCast(acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead), _
                LayerTable)
            '// returns the layer table for the current database
            Dim acBTab As BlockTable = _
            DirectCast(acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead), _
                BlockTable)
            '// returns the text style table for the current database
            Dim acTxSTab As TextStyleTable = _
            DirectCast(acTrans.GetObject(acCurDb.TextStyleTableId, OpenMode.ForRead), _
                TextStyleTable)


            '// Layers
            Me.cbLyCode.Text = CM_3DPoints.Stgs.lyCode
            Me.cbLyMark.Text = CM_3DPoints.Stgs.lyMark
            Me.cbLyNr.Text = CM_3DPoints.Stgs.lyNr
            Me.cbLyZ.Text = CM_3DPoints.Stgs.lyZ

            '' Step through the Layer table and get each layer name
            For Each acObjId As ObjectId In acLyrTbl
                Dim acLyrTblRec As LayerTableRecord
                acLyrTblRec = _
                    DirectCast(acTrans.GetObject(acObjId, OpenMode.ForRead), _
                    LayerTableRecord)

                Me.cbLyMark.Items.Add(acLyrTblRec.Name)
                Me.cbLyNr.Items.Add(acLyrTblRec.Name)
                Me.cbLyZ.Items.Add(acLyrTblRec.Name)
                Me.cbLyCode.Items.Add(acLyrTblRec.Name)
            Next


            '// Block
            Me.CB_BlkName.Text = CM_3DPoints.Stgs.sBlockName

                '// Step through the block table and print each name
			For Each acObjId As ObjectId In acBTab
				Dim acBEntry As BlockTableRecord = _
				DirectCast(acTrans.GetObject(acObjId, OpenMode.ForRead), _
				    BlockTableRecord)

				If acBEntry.Name.Substring(0, 1) <> "*" Then
					If "_None" <> acBEntry.Name Then
						CB_BlkName.Items.Add(acBEntry.Name)
					End If
				End If
			Next

            '// Text
			For Each acObjId As ObjectId In acTxSTab
                Dim acTxSty As TextStyleTableRecord = _
				DirectCast(acTrans.GetObject(acObjId, OpenMode.ForRead), _
				    TextStyleTableRecord)
                Me.CB_TxSty.Items.Add(acTxSty.Name)
			Next
            If CM_3DPoints.Stgs.sTxSty <> Nothing andalso _
                CM_3DPoints.Stgs.sTxSty <> "" Then

                If acTxSTab.Has(CM_3DPoints.Stgs.sTxSty) Then
                    Me.CB_TxSty.Text = CM_3DPoints.Stgs.sTxSty
                Else
                    Dim acTxSty As TextStyleTableRecord = _
				    DirectCast(acTrans.GetObject(acTxSTab("Standard"), OpenMode.ForRead), _
				        TextStyleTableRecord)
                    acTxSTab.UpgradeOpen()
                    
                    Dim acNTxS As TextStyleTableRecord = _
                        DirectCast(acTxSty.Clone(), TextStyleTableRecord)
                    acNTxS.Name = CM_3DPoints.Stgs.sTxSty
                    acTxSTab.Add(acNTxS)
                    acTrans.AddNewlyCreatedDBObject(acNTxS,True)
                    Me.CB_TxSty.Text = CM_3DPoints.Stgs.sTxSty
                End If
                
            End If
            Me.TX_TXH.Text = CM_3DPoints.Stgs.txH.ToString("0.000")

            Select Case CM_3DPoints.Stgs.iRstPos
                Case 0
                    Me.CK_DontReset.Checked = True
                Case 1
                    Me.CK_ResetPos.Checked = True
                Case 2
                    Me.CK_OnlyMoved.Checked = True
                Case 3
                    Me.CK_SmartReset.Checked = True
            End Select
            
            
            acTrans.Commit()
        Catch ex As Exception
            acTrans.Abort()
            CMain.DebugException(ex)
            Me.Close()
        End Try
        End Using '' Dispose of the transaction

    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub TX_TXH_Validating( _
        ByVal sender As System.Object,  _
        ByVal e As System.ComponentModel.CancelEventArgs) _
        Handles TX_TXH.Validating
        
        Dim d As Double
        Try
            d = CDbl(TX_TXH.Text)
        Catch ex As System.Exception
             MsgBox(CMain._T(TX.ENOTNUM))
             e.Cancel = True
             TX_TXH.Select(0, TX_TXH.Text.Length)
        End Try
    End Sub
    '// ===================================================================== '


#End Region
    ' /////////////////////////////////////////////////////////////////////////

End Class

'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
